import {createSlice} from '@reduxjs/toolkit'

const tabSlice = createSlice({
    name: 'tab',
    // 初始值
    initialState: {
        isCollapse: false,
        tabList: [{
            path: '/home',
            name: 'home',
            label: '首页',
            icon: 'HomeOutlined',
            url: '/Home/index'
        }],
        currentTabPath: '/home'
    },
    // 修改值的方法
    reducers: {
        collapseMenu: state => {
            state.isCollapse = !state.isCollapse
        },
        setTabList: (state, _data) => {
            const {data, type} = _data.payload
            let index = state.tabList.findIndex(item => item.path === data.path)
            if (type === 'add' && index === -1) state.tabList.push(data)
            if (type === 'close' && index !== -1) state.tabList.splice(index, 1)

        },
        setCurrentTabPath: (state, data) => {
            const {payload} = data
            state.currentTabPath = payload
        }
    }
})

// 暴露改变值的方法
export const {collapseMenu, setTabList, setCurrentTabPath} = tabSlice.actions
// 暴露模块
export default tabSlice.reducer